﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RecipeBookBusiness
{
    public class User
    {
        private RecipeBookEntities _entities = new RecipeBookEntities();
        private int _id;
        private string _firstName;
        private string _lastName;
        private string _email;
        private string _password;
        private List<int> _favoriteRecipeIDs;
        private string _username;

        public User(int userID)
        {
            loadUserClass(userID);
        }

        public User(string firstName, string lastName, string email, string password, string username)
        {
            try
            {
                UserDataRow udr = new UserDataRow();
                udr.FName = firstName;
                udr.LName = lastName;
                udr.EmailAddress = email;
                udr.Password = password;
                udr.Username = username;

                //Save to DB and get ID
                _entities.AddToUserDataRowSet(udr);
                _entities.SaveChanges();
                int newUserID = 0;
                foreach (UserDataRow idUdr in _entities.UserDataRowSet)
                {
                    if (idUdr.ID > newUserID)
                        newUserID = idUdr.ID;
                }
                loadUserClass(newUserID);
            }
            catch (Exception ex)
            {
                //TODO: Real exception handling
                Console.Write(ex.Message);
            }
        }

        private void loadUserClass(int userID)
        {
            UserDataRow userDR = (from c in _entities.UserDataRowSet
                                          where c.ID == userID
                                          select c).FirstOrDefault();
            _id = userDR.ID;
            _firstName = userDR.FName;
            _lastName = userDR.LName;
            _email = userDR.EmailAddress;
            _password = userDR.Password;
            _username = userDR.Username;

            _favoriteRecipeIDs = new List<int>();
            foreach (UserRecipeDataRow urdr in _entities.UserRecipeDataRowSet)
            {
                if (urdr.IDUser == _id)
                {
                    _favoriteRecipeIDs.Add(urdr.IDRecipe);
                }
            }
        }

        public void UpdateUser(string firstName, string lastName, string email)
        {
            UserDataRow userDR = (from c in _entities.UserDataRowSet
                                      where c.ID == _id
                                      select c).FirstOrDefault();
            bool needToUpdate = false;
            if (!_firstName.Equals(firstName))
            {
                _firstName = firstName;
                needToUpdate = true;
            }
            if (!_lastName.Equals(lastName))
            {
                _lastName = lastName;
                needToUpdate = true;
            }
            if (!_email.Equals(email))
            {
                _email = email;
                needToUpdate = true;
            }

            if (needToUpdate)
            {
                userDR.FName = _firstName;
                userDR.LName = _lastName;
                userDR.EmailAddress = _email;
                _entities.SaveChanges();
            }
        }

        #region Recipe methods
        public void AddRecipe(int recipeID)
        {
            try
            {
                UserRecipeDataRow urdr = new UserRecipeDataRow();
                urdr.IDUser = _id;
                urdr.IDRecipe = recipeID;
                _entities.AddToUserRecipeDataRowSet(urdr);
                _entities.SaveChanges();

                _favoriteRecipeIDs.Add(recipeID);
            }
            catch (Exception ex)
            {
                //TODO: Return real error
                Console.Write(ex.Message);
            }
        }

        public void RemoveRecipe(int recipeID)
        {
            try
            {
                int indexToRemove = -1;
                foreach (int rID in _favoriteRecipeIDs)
                {
                    if (rID == recipeID)
                    {
                        indexToRemove = _favoriteRecipeIDs.IndexOf(rID);
                    }
                }

                if (indexToRemove == -1)
                    return; //error - recipe not found

                //Remove using SQL
                IQueryable<UserRecipeDataRow> urQuery =
                    from ur in _entities.UserRecipeDataRowSet
                    where ur.IDUser == _id
                    where ur.IDRecipe == recipeID
                    select ur;
                List<UserRecipeDataRow> selectedUr = urQuery.ToList();
                if (selectedUr != null && selectedUr.Count > 0)
                {
                    UserRecipeDataRow urToDelete = selectedUr.First();
                    _entities.DeleteObject(urToDelete);
                    _entities.SaveChanges();
                }
                else
                {
                    return; //error - user-recipe not found
                }


                //Remove from object
                _favoriteRecipeIDs.RemoveAt(indexToRemove);
            }
            catch (Exception ex)
            {
                //TODO: Return real error
                Console.Write(ex.Message);
            }
        }
        #endregion

        public int ID
        {
            get
            {
                return _id;
            }
        }

        public string FirstName
        {
            get
            {
                return _firstName;
            }
        }

        public string LastName
        {
            get
            {
                return _lastName;
            }
        }

        public string EmailAddress
        {
            get
            {
                return _email;
            }
        }

        public string Password
        {
            get
            {
                return _password;
            }
        }

        public List<int> FavoriteRecipeIDs
        {
            get
            {
                return _favoriteRecipeIDs;
            }
        }

        public string Username
        {
            get
            {
                return _username;
            }
        }
    }
}
